import React, {Component} from 'react'
import PropTypes from 'prop-types'
import NotFound from "../../pages/other/NotFound";
import {Route,Switch} from 'react-router-dom'
import { KeepAlive } from 'react-keep-alive';
export default class extends Component {
  static propTypes = {
    routes: PropTypes.array.isRequired,
    isRoot: PropTypes.bool
  };

  constructor(props) {
    super(props);
    this.state = {}
  }

  render() {
    return (
      <Switch>
        {
          this.props.routes.map(({path,name, componentName: ComponentName, exact = true, routes = []}, key) => {
            return <Route
              exact={routes.length !== 0 && exact}
              path={path}
              key={key}
              render={props =>
                <KeepAlive name={name}>
                  <div>
                    <ComponentName {...props} routes={routes}/>
                  </div>
                </KeepAlive>
              }
            />
          })
        }
        {this.props.isRoot ? <Route component={NotFound}/> : null}
      </Switch>
    )
  }
}
